//
//  YZMStockApplyMoneyViewController.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/11/6.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMStockApplyMoneyViewController.h"
#import "YZMGridView.h"
#import "YZMStockUnitView.h"
#import "YZMSegmentForChoose.h"
#import "YZMGridMoneyModel.h"
#import "YZMRecommandView.h"
#import "YZMWebViewService.h"
#import "YZMSegmentViewFixed.h"
#import "UIViewController+Utils.h"
#import "YZMConfigureService.h"
#import "NSNumber+Utils.h"
#import "YZMAlertView.h"
#import "YZMDelayCharge.h"

#define timeWidth  (ScreenWidth - 30)

static const int column = 4;

@interface YZMStockApplyMoneyViewController () <UITextFieldDelegate,YZMSegmentViewDelegate>

@property (nonatomic, strong) YZMGridView *gridView;
@property (nonatomic, strong) UILabel *stockNumberLB;
@property (nonatomic, strong) UILabel *stockTimeLB;
@property (nonatomic, strong) UIButton *submitBT;
@property (nonatomic, strong) NSDate *stockFetchDate;

@property (nonatomic, assign) double buyMoney;

@property (nonatomic, strong) NSString *quitLossMoney;
@property (nonatomic, strong) NSString *quitGainMoney;
@property (nonatomic, strong) NSString *principal;
@property (nonatomic, strong) NSString *serviceCharge;

@property (nonatomic, assign) NSInteger stockCount;
@property (nonatomic, assign) BOOL needReloadFlag;
@property (nonatomic, assign) double stockAmountStatistics;
@property (nonatomic, assign) double stockAmountHasUse;
@property (nonatomic ,strong) YZMSegmentViewFixed *segView;
@property (nonatomic ,strong) UIView *moneyView;
@property (nonatomic, strong) UIView *otherMoneyViwe;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *otherBottomView;
@property (nonatomic, strong) YZMConfigureModel *configureModel;
@property (nonatomic ,strong) UIScrollView *scrollView;
@property (nonatomic ,strong) UIScrollView *otherScrollView;
@property (nonatomic ,strong) UITextField *otherField;
@property (nonatomic ,strong) NSString *positionDay;
@property (nonatomic, strong) NSString *otherPositionDay;
@property (nonatomic, strong) YZMGridMoneyModel *gridMoneyModel;

@property (nonatomic, strong) UIButton *priOneBtn;

@property (nonatomic, strong) UIButton *priTwoBtn;

@property (nonatomic, assign) NSInteger scrollowIndex;

@end

@implementation YZMStockApplyMoneyViewController

- (instancetype)init {
    
    if (self = [super init]) {
        
//        self.gridMoneyModel = self.configureModel.tradingMoneyList[0];
    }
    
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    
    self.title = @"T+D点买A股";
    if (!YZMUserServiceInstance.user.detail.isNamed) {
        
        YZMAlertView *alertView = [YZMAlertView alertViewForRecommandWithContent:@"您还未实名认证，请先认证"];
        alertView.cancelButton.hidden = NO;
        [alertView.sureButton setTitle:@"立即验证" forState:UIControlStateNormal];
        alertView.cancleHandle = ^{
            
            [YZMHCMInstance popViewController:nil animated:YES];
        };
        alertView.confirmHandle  = ^{
            
            [YZMHCMInstance pushViewControllerWithName:@"identifyVerify" andParams:nil];
            return YES;
        };
        [alertView showInView:self.navigationController.view];
        
        return;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear: YES];
    self.title = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    self.configureModel = [[YZMTDConfigureService sharedService].defaultConfigure copy];
    self.navigationItem.rightBarButtonItem = [YZMAppStyle rightBarItemWithTitle:@"规则" target:self action:@selector(rightBarItemPressed:)];
    [self createObjects];
    [self registerForKeyboardAutoWithBottomView:self.bottomView];
    if (self.configureModel) {
        
        [self refreshView];
    }
    [self fetchStockBuyConfigure];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView *)timeDurationViewWithType:(NSInteger)index{
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [YZMAppStyle colorForMain];
    UIView *iv = [UIView new];
    [YZMAppStyle addLeftLineViewWithParentView:iv color:[YZMAppStyle colorForSplitLine]];
    [bgView addSubview:iv];
    [iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(15);
        make.right.equalTo(bgView).offset(-15);
        make.top.bottom.equalTo(bgView);
    }];
    iv.backgroundColor = [YZMAppStyle colorForMain];
  __block  UIButton *priBtn;
    [self.configureModel.holdDayList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn setBackgroundImage:[UIImage createImageWithColor:[YZMAppStyle buttonSelectedColor]] forState:UIControlStateSelected];
        [btn setTitleColor:[YZMAppStyle colorForGrayText] forState:UIControlStateNormal];
        [btn setTitleColor:[YZMAppStyle colorForMain] forState:UIControlStateSelected];
       [btn.titleLabel setFont:[YZMAppStyle largeSizeFont]];
        [btn setTitle:[NSString stringWithFormat:@"%@天", self.configureModel.holdDayList[idx]] forState:UIControlStateNormal];
        if (index) {
            btn.tag = idx + 30000;
            if (idx == 0) {
                self.priTwoBtn = btn;
                self.priTwoBtn.selected = YES;
            }
        }else{
            btn.tag = idx + 20000;
            if (idx == 0) {
                self.priOneBtn = btn;
                self.priOneBtn.selected = YES;
            }
        }
        [btn addTarget:self action:@selector(chooseDataMethod:) forControlEvents:UIControlEventTouchUpInside];
        [iv addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (priBtn) {
                if (idx % column == 0) {
                    make.left.equalTo(iv);
                    make.top.equalTo(priBtn.mas_bottom);
                    make.width.mas_equalTo(timeWidth / column);
                    make.height.mas_equalTo(54);
                }else{
                    make.left.equalTo(priBtn.mas_right);
                    make.top.equalTo(priBtn.mas_top);
                    make.width.mas_equalTo(timeWidth / column);
                    make.height.mas_equalTo(54);
                }
                if (idx == self.configureModel.holdDayList.count - 1) {
                    make.bottom.equalTo(iv);
                }
            }else{
                make.left.equalTo(iv);
                make.top.equalTo(iv);
                make.width.mas_equalTo(timeWidth / column);
                make.height.mas_equalTo(54);
            }
            UIView *ivLineRight = [UIView new];
            ivLineRight.backgroundColor = [YZMAppStyle colorForSplitLine];
            [btn addSubview:ivLineRight];
            [ivLineRight mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.top.bottom.equalTo(btn);
                make.width.mas_equalTo(0.5);
            }];
            
            UIView *ivLineBottom = [UIView new];
            ivLineBottom.backgroundColor = [YZMAppStyle colorForSplitLine];
            [btn addSubview:ivLineBottom];
            [ivLineBottom mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.equalTo(btn);
                make.height.mas_equalTo(0.5);
            }];
            
            
        }];
        priBtn = btn;
    }];
    
    return bgView;
}

#pragma mark -- UI

- (void)createObjects{
    __weak __typeof(&*self) weakSelf = self;
    
    UIView *bottomBtnView = [UIView new];
    bottomBtnView.backgroundColor = [YZMAppStyle appBackgroundColor];
    [weakSelf.view addSubview:bottomBtnView];
    
    [bottomBtnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(weakSelf.view);
        make.left.equalTo(weakSelf.view);
    }];
    
    UIView *lineView = [YZMAppStyle splitLine];
    [bottomBtnView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(bottomBtnView);
    }];
    
    self.submitBT = [YZMAppStyle submitButton];
    self.submitBT.enabled = YES;
    [self.submitBT setTitle:@"下一步" forState:UIControlStateNormal];
    [self.submitBT addTarget:self action:@selector(clickToBuyStock:) forControlEvents:UIControlEventTouchUpInside];
    [bottomBtnView addSubview:self.submitBT];
    [self.submitBT mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(bottomBtnView).insets(UIEdgeInsetsMake(12, 15, 12, 15));
    }];

    self.segView = [YZMSegmentViewFixed new];
    [self.view addSubview:self.segView];
    [self.segView mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.top.equalTo(weakSelf.view).offset(14);
        make.left.right.equalTo(weakSelf.view);
        make.bottom.equalTo(bottomBtnView.mas_top);
    }];
    
    self.segView.titleArray = @[@"点买金额",@"其它金额"];
    self.segView.delegate = self;
    self.moneyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    
    self.otherMoneyViwe = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
    
    self.segView.viewArray = @[self.moneyView, self.otherMoneyViwe];
    
    //点买金额
    self.bottomView = [UIView new];
    [self.moneyView addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.bottom.equalTo(weakSelf.moneyView);
        maker.height.mas_equalTo(0);
    }];
    
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [YZMAppStyle appBackgroundColor];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.moneyView addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.equalTo(weakSelf.moneyView);
        make.bottom.equalTo(weakSelf.bottomView.mas_top);
    }];
    
    UIView *containerView = [UIView new];
    [self.scrollView addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.edges.equalTo(weakSelf.scrollView);
        make.width.equalTo(weakSelf.scrollView);
    }];
    
    //点买金额GridView
    UIView *gridMoneyView = [self getGridMoneyView];
    [containerView addSubview:gridMoneyView];
    [gridMoneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(containerView);
        make.left.right.equalTo(containerView);
    }];
    
    [self generalView:gridMoneyView addView:containerView withType:0];
    
    //其它金额
    self.otherBottomView = [UIView new];
    [self.otherMoneyViwe addSubview:self.otherBottomView];
    [self.otherBottomView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.right.bottom.equalTo(weakSelf.otherMoneyViwe);
        maker.height.mas_equalTo(0);
    }];
    
    self.otherScrollView = [[UIScrollView alloc] init];
    self.otherScrollView.backgroundColor = [YZMAppStyle appBackgroundColor];
    self.otherScrollView.showsVerticalScrollIndicator = NO;
    [self.otherMoneyViwe addSubview:self.otherScrollView];
    [self.otherScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.equalTo(weakSelf.otherMoneyViwe);
        make.bottom.equalTo(weakSelf.otherBottomView.mas_top);
    }];

    UIView *otherContainerView = [UIView new];
    [self.otherScrollView addSubview:otherContainerView];
    [otherContainerView mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.edges.equalTo(weakSelf.otherScrollView);
        make.width.equalTo(weakSelf.otherScrollView);
    }];
    
    UIView *topView = [[UIView alloc] init];
    [otherContainerView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.equalTo(otherContainerView);
        make.right.equalTo(otherContainerView);
//        make.height.mas_equalTo(70);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [YZMAppStyle colorForMain];
    [topView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.equalTo(topView);
        make.height.mas_equalTo(50);
    }];
    
    self.otherField = [[UITextField alloc] init];
    self.otherField.textColor = [YZMAppStyle colorForWhite];
    self.otherField.placeholder = @"最少1万，最多100万";
    [self.otherField setValue:[YZMAppStyle colorForGrayText] forKeyPath:@"_placeholderLabel.textColor"];
    [self.otherField setValue:[YZMAppStyle mediumSizeFont] forKeyPath:@"_placeholderLabel.font"];
    self.otherField.delegate = self;
    self.otherField.keyboardType = UIKeyboardTypeNumberPad;
    [bgView addSubview:self.otherField];
    [self.otherField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(bgView);
        make.left.equalTo(bgView).offset(15);
        make.right.equalTo(bgView).offset(-55);
    }];
    
    UILabel *yuanLb = [YZMAppStyle labelWithLargeWhite];
    yuanLb.text = @"万元";
    yuanLb.backgroundColor = [YZMAppStyle colorForMain];
    [topView addSubview:yuanLb];
    [yuanLb mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.height.equalTo(weakSelf.otherField);
        make.left.equalTo(weakSelf.otherField.mas_right);
        make.right.equalTo(topView.mas_right).offset(-15);
    }];
    
    UILabel *remark = [YZMAppStyle labelWithSmallLightGray];
    remark.text = @" 注：输入的点买金额必须是1万的整数倍";
    remark.textColor = [YZMAppStyle colorForRiseText];
    remark.backgroundColor = [YZMAppStyle appBackgroundColor];
    [topView addSubview:remark];
    [remark mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.otherField.mas_bottom).offset(10);
        make.right.equalTo(topView);
        make.left.equalTo(topView).offset(15);
        make.bottom.equalTo(topView).offset(-10);
    }];
    
    [self generalView:topView addView:otherContainerView withType:1];
}

- (void)generalView:(UIView *)view addView:(UIView *)addView withType:(NSInteger)index {
    
    __weak __typeof(&*self) weakSelf = self;
    
//    UIView *rightView = [[UIView alloc] init];
//
//    UIButton *shortDateBtn = [YZMAppStyle chooseDateBtn];
//    shortDateBtn.selected = YES;
//    shortDateBtn.hidden = YES;
//    if (index) {
//
//        shortDateBtn.tag = 150;
//    } else {
//
//        shortDateBtn.tag = 151;
//    }
//    [shortDateBtn setTitle:@"--" forState:UIControlStateNormal];
//    [shortDateBtn addTarget:self action:@selector(chooseDataMethod:) forControlEvents:UIControlEventTouchUpInside];
//    [rightView addSubview:shortDateBtn];
//    [shortDateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        make.left.top.bottom.centerY.equalTo(rightView);
//        if (iPhone4 || iPhone5) {
//
//            make.width.mas_equalTo(58);
//        } else {
//
//            make.width.mas_equalTo(70);
//        }
//        make.height.mas_equalTo(22);
//    }];
//
//    UIButton *middDateBtn = [YZMAppStyle chooseDateBtn];
//    middDateBtn.selected = NO;
//    middDateBtn.hidden = YES;
//    if (index) {
//
//        middDateBtn.tag = 170;
//    } else {
//
//        middDateBtn.tag = 171;
//    }
//    [middDateBtn setTitle:@"--" forState:UIControlStateNormal];
//    [middDateBtn addTarget:self action:@selector(chooseDataMethod:) forControlEvents:UIControlEventTouchUpInside];
//    [rightView addSubview:middDateBtn];
//    [middDateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        if (iPhone4 || iPhone5) {
//
//            make.left.equalTo(shortDateBtn.mas_right).offset(8);
//            make.width.mas_equalTo(58);
//        } else {
//
//            make.left.equalTo(shortDateBtn.mas_right).offset(12);
//            make.width.mas_equalTo(70);
//        }
//        make.height.mas_equalTo(22);
//        make.top.bottom.centerY.equalTo(rightView);
//    }];
//
//    UIButton *longDateBtn = [YZMAppStyle chooseDateBtn];
//    longDateBtn.selected = NO;
//    longDateBtn.hidden = YES;
//    if (index) {
//
//        longDateBtn.tag = 160;
//    } else {
//
//        longDateBtn.tag = 161;
//    }
//    [longDateBtn setTitle:@"--" forState:UIControlStateNormal];
//    [longDateBtn addTarget:self action:@selector(chooseDataMethod:) forControlEvents:UIControlEventTouchUpInside];
//    [rightView addSubview:longDateBtn];
//    [longDateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        if (iPhone4 || iPhone5) {
//
//            make.left.equalTo(middDateBtn.mas_right).offset(8);
//            make.width.mas_equalTo(58);
//        } else {
//
//            make.left.equalTo(middDateBtn.mas_right).offset(12);
//            make.width.mas_equalTo(70);
//        }
//        make.height.mas_equalTo(22);
//        make.right.top.bottom.centerY.equalTo(rightView);
//    }];
//
    //持仓时间
    YZMStockUnitView *timeView = [YZMStockUnitView new];
    timeView.title = @"持仓时间";
//    [ivRight mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(ScreenWidth + 20);
//    }];
//    timeView.rightLargeView = ivRight;
    timeView.bottomLineType = YZMStockUnitLineTypeForMarginLeft;
    timeView.explainAction = ^(){
        
        [weakSelf clickToShowTimeRecommand];
    };
    [timeView.explainBT setImage:nil forState:UIControlStateNormal];
    [timeView.explainBT setTitle:@"(交易日)" forState:UIControlStateNormal];
    timeView.explainBT.titleLabel.font = [YZMAppStyle mediumSizeFont];
    [timeView.explainBT mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(65);
    }];
    [addView addSubview:timeView];
    [timeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom);
        make.left.right.equalTo(addView);
        make.height.mas_equalTo(@45);
    }];
    
    UIView *ivRight = [self timeDurationViewWithType:index];
    
    [addView addSubview:ivRight];
    [ivRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeView.mas_bottom);
        make.left.right.equalTo(addView);
    }];
    //触发止盈
    UILabel *stopProfit = [YZMAppStyle labelWithLargeLightGray];
    stopProfit.text = @"--";
    if (index) {
        
        stopProfit.tag = 100;
    } else {
        
        stopProfit.tag = 101;
    }
    
    YZMStockUnitView *profitView = [YZMStockUnitView new];
    profitView.title = @"触发止盈";
    profitView.rightView = stopProfit;
    profitView.bottomLineType = YZMStockUnitLineTypeForMarginLeft;
    profitView.explainAction = ^(){
        
        [weakSelf clickToShowProfitRecommand];
    };
    [addView addSubview:profitView];
    [profitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ivRight.mas_bottom);
        make.left.right.equalTo(addView);
    }];
    
    //触发止损
    UILabel *stopLoss = [YZMAppStyle labelWithLargeLightGray];
    stopLoss.text = @"--";
    if (index) {
        
        stopLoss.tag = 110;
    } else {
        
        stopLoss.tag = 111;
    }
    
    YZMStockUnitView *lossView = [YZMStockUnitView new];
    lossView.title = @"触发止损";
    lossView.rightView = stopLoss;
    lossView.explainAction = ^(){
        
        [weakSelf clickToShowLossRecommand];
    };
    [addView addSubview:lossView];
    [lossView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(profitView.mas_bottom);
        make.left.right.equalTo(addView);
    }];
    
    UIView *splitView = [UIView new];
    splitView.backgroundColor = [YZMAppStyle appBackgroundColor];
    [addView addSubview:splitView];
    [splitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lossView.mas_bottom);
        make.left.right.equalTo(addView);
        make.height.mas_equalTo(16);
    }];
    
    //递延条件
    UILabel *deferConditionLB = [YZMAppStyle labelWithLargeLightGray];
    deferConditionLB.text = @"--";
    if (index) {
        
        deferConditionLB.tag = 120;
    } else {
        
        deferConditionLB.tag = 121;
    }
    
    YZMStockUnitView *managementFeeView = [YZMStockUnitView new];
    managementFeeView.title = @"递延条件";
    managementFeeView.rightView = deferConditionLB;
    managementFeeView.bottomLineType = YZMStockUnitLineTypeForMarginLeft;
    managementFeeView.explainAction = ^(){
        
        [weakSelf clickToShowManagementFeeRecommand];
    };
    [addView addSubview:managementFeeView];
    [managementFeeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(splitView.mas_bottom);
        make.left.right.equalTo(addView);
    }];
    
    //盈利分成
    UILabel *profitShareLB = [YZMAppStyle labelWithLargeLightGray];
    profitShareLB.text = @"--";
    if (index) {
        
        profitShareLB.tag = 130;
    } else {
        
        profitShareLB.tag = 131;
    }
    
    YZMStockUnitView *bondView = [YZMStockUnitView new];
    bondView.title = @"盈利分成";
    bondView.rightView = profitShareLB;
    bondView.explainAction = ^(){
        
        [weakSelf clickToShowBondRecommand];
    };
    [addView addSubview:bondView];
    [bondView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(addView);
        make.top.equalTo(managementFeeView.mas_bottom);
    }];
    
    UIView *tradeRuleView = [[UIView alloc] init];
    [addView addSubview:tradeRuleView];
    [tradeRuleView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.equalTo(addView);
        make.top.equalTo(bondView.mas_bottom);
        make.bottom.equalTo(addView);
        make.height.mas_equalTo(160);
    }];
    
    UILabel *ruleLB = [YZMAppStyle labelWithSmallLightGray];
    
    if (index) {
        
        ruleLB.tag = 200;
    } else {
        
        ruleLB.tag = 201;
    }
    
    ruleLB.text = [NSString stringWithFormat:@"交易规则\n点买须知：单只股票金额不能超过50万；\n交易限制：不得购买S、ST、*ST、S*ST、SST、以及被交易所特别处理的股票；\n收益分配：操盘利润90%%归点买人，10%%归投资人。"];
    ruleLB.numberOfLines = 0;
    
    [addView addSubview:ruleLB];
    [ruleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(tradeRuleView).offset(15);
        make.left.equalTo(tradeRuleView).offset(10);
        make.right.equalTo(tradeRuleView).offset(-20);
    }];
    
    self.gridView.clickAction = ^(NSInteger index, YZMGridMoneyModel *data){
        
        weakSelf.gridMoneyModel = weakSelf.configureModel.tradingMoneyList[index];
        weakSelf.buyMoney = weakSelf.gridMoneyModel.money;
    };
}

- (UIView *)getGridMoneyView{
    
    UIView *view = [UIView new];
    view.backgroundColor = [YZMAppStyle colorForMain];
    
    self.gridView = [YZMGridView new];
    [view addSubview:self.gridView];
    
    [self.gridView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(view).offset(15);
        make.right.equalTo(view).offset(-15);
        make.bottom.equalTo(view);
    }];
    
    return view;
}

- (void)refreshView {
    
    UILabel *stopProfit = [self.view viewWithTag:100];
    UILabel *stopLoss = [self.view viewWithTag:110];
    UILabel *deferConditionLB = [self.view viewWithTag:120];
    UILabel *profitShareLB = [self.view viewWithTag:130];
    UIButton *shortDateBtn = [self.view viewWithTag:150];
    UIButton *longDateBtn = [self.view viewWithTag:160];
    UILabel *ruleLB = [self.view viewWithTag:200];
    UIButton *middDateBtn = [self.view viewWithTag:170];
    
    ruleLB.text = [NSString stringWithFormat:@"交易规则\n点买须知：单只股票金额不能超过%@万；\n交易限制：不得购买S、ST、*ST、S*ST、SST、以及被交易所特别处理的股票；\n收益分配：操盘利润%@%%归点买人，%@%%归投资人。", @(self.configureModel.maxMoneyOneStock / 10000).r4fString, @(self.configureModel.profitShare * 100).rfString, @(100 - self.configureModel.profitShare * 100).rfString];
    
    self.gridView.dataList = self.configureModel.tradingMoneyList;
    self.buyMoney = [self.gridView.dataList[0] money];
    stopProfit.text = [NSString stringWithFormat:@"%@%%", @(self.configureModel.quitGainRatio)];
    stopLoss.text = [NSString stringWithFormat:@"%@%%", @(self.configureModel.quitLossRatio)];
    deferConditionLB.text = [NSString stringWithFormat:@"%@%%", @(self.configureModel.deferConditionRatio)];
    profitShareLB.text = [NSString stringWithFormat:@"%@%%", @(self.configureModel.profitShare * 100).rfString];
//    if (self.configureModel.holdDayList.count == 3) {
//
//        shortDateBtn.hidden = NO;
//        longDateBtn.hidden = NO;
//        middDateBtn.hidden = NO;
//        [shortDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[0]] forState:UIControlStateNormal];
//        [longDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[2]] forState:UIControlStateNormal];
//        [middDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[1]] forState:UIControlStateNormal];
//    } else if (self.configureModel.holdDayList.count == 2) {
//
//        shortDateBtn.hidden = YES;
//        longDateBtn.hidden = NO;
//        middDateBtn.hidden = NO;
//        [middDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[0]] forState:UIControlStateNormal];
//        [longDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[1]] forState:UIControlStateNormal];
//    } else if (self.configureModel.holdDayList.count == 1) {
//
//        longDateBtn.selected = YES;
//        shortDateBtn.hidden = YES;
//        longDateBtn.hidden = NO;
//        middDateBtn.hidden = YES;
//        [longDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[0]] forState:UIControlStateNormal];
//    }
    
    stopProfit = [self.view viewWithTag:101];
    stopLoss = [self.view viewWithTag:111];
    deferConditionLB = [self.view viewWithTag:121];
    profitShareLB = [self.view viewWithTag:131];
    shortDateBtn = [self.view viewWithTag:151];
    longDateBtn = [self.view viewWithTag:161];
    ruleLB = [self.view viewWithTag:201];
    middDateBtn = [self.view viewWithTag:171];
    
    ruleLB.text = [NSString stringWithFormat:@"交易规则\n点买须知：单只股票金额不能超过%@万；\n交易限制：不得购买S、ST、*ST、S*ST、SST、以及被交易所特别处理的股票；\n收益分配：操盘利润%@%%归点买人，%@%%归投资人。", @(self.configureModel.maxMoneyOneStock / 10000).r4fString, @(self.configureModel.profitShare * 100).rfString, @(100 - self.configureModel.profitShare * 100).rfString];
    
    stopProfit.text = [NSString stringWithFormat:@"%@%%", @(self.configureModel.quitGainRatio)];
    stopLoss.text = [NSString stringWithFormat:@"%@%%", @(self.configureModel.quitLossRatio)];
    deferConditionLB.text = [NSString stringWithFormat:@"%@%%", @(self.configureModel.deferConditionRatio)];
    profitShareLB.text = [NSString stringWithFormat:@"%@%%", @(self.configureModel.profitShare * 100).rfString];
    
//    if (self.configureModel.holdDayList.count == 3) {
//
//        shortDateBtn.hidden = NO;
//        longDateBtn.hidden = NO;
//        middDateBtn.hidden = NO;
//        [shortDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[0]] forState:UIControlStateNormal];
//        [longDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[2]] forState:UIControlStateNormal];
//        [middDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[1]] forState:UIControlStateNormal];
//    } else if (self.configureModel.holdDayList.count == 2) {
//
//        shortDateBtn.hidden = YES;
//        longDateBtn.hidden = NO;
//        middDateBtn.hidden = NO;
//        [middDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[0]] forState:UIControlStateNormal];
//        [longDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[1]] forState:UIControlStateNormal];
//    } else if (self.configureModel.holdDayList.count == 1) {
//
//        longDateBtn.selected = YES;
//        shortDateBtn.hidden = YES;
//        longDateBtn.hidden = NO;
//        middDateBtn.hidden = YES;
//        [longDateBtn setTitle:[NSString stringWithFormat:@"T+%@", self.configureModel.holdDayList[0]] forState:UIControlStateNormal];
//    }
    
    YZMGridMoneyModel *model = [self.configureModel.tradingMoneyList lastObject];
    self.otherField.placeholder = [NSString stringWithFormat:@"最少1万，最多%@万",  @(model.money / 10000).rfString];
    self.positionDay = self.configureModel.holdDayList[0];
    self.otherPositionDay =  self.configureModel.holdDayList[0];
}

#pragma mark -- event
//选择持仓时间
- (void)chooseDataMethod:(UIButton *)send {
    NSInteger index;
    if (send.tag >= 30000) {
        if (self.priTwoBtn) {
            self.priTwoBtn.selected = NO;
        }
        index = send.tag - 30000;
        self.priTwoBtn = send;
        self.otherPositionDay = self.configureModel.holdDayList[index];
    }else{
        if (self.priOneBtn) {
            self.priOneBtn.selected = NO;
        }
        index = send.tag - 20000;
        self.priOneBtn = send;
         self.positionDay = self.configureModel.holdDayList[index];
    }
     send.selected = YES;
    
   
    return;
//    UIButton *shortDateBtn = [self.view viewWithTag:150];
//    UIButton *longDateBtn = [self.view viewWithTag:160];
//    UIButton *middDateBtn = [self.view viewWithTag:170];
//
//    UIButton *shortTDateBtn = [self.view viewWithTag:151];
//    UIButton *longTDateBtn = [self.view viewWithTag:161];
//    UIButton *middTDateBtn = [self.view viewWithTag:171];
//
//    send.selected = YES;
//
//    if (send == shortDateBtn || send == shortTDateBtn) {
//
//        longDateBtn.selected = NO;
//        longTDateBtn.selected = NO;
//        middDateBtn.selected = NO;
//        middTDateBtn.selected = NO;
//        shortDateBtn.selected = YES;
//        shortTDateBtn.selected = YES;
//        self.positionDay = self.configureModel.holdDayList[0];
//    } else if (send == middDateBtn || send == middTDateBtn) {
//
//        longDateBtn.selected = NO;
//        longTDateBtn.selected = NO;
//        shortDateBtn.selected = NO;
//        shortTDateBtn.selected = NO;
//        middDateBtn.selected = YES;
//        middTDateBtn.selected = YES;
//        if (self.configureModel.holdDayList.count == 1 || self.configureModel.holdDayList.count == 2) {
//
//            self.positionDay = self.configureModel.holdDayList[0];
//        } else if (self.configureModel.holdDayList.count == 3) {
//
//            self.positionDay = self.configureModel.holdDayList[1];
//        }
//    } else {
//
//        longDateBtn.selected = YES;
//        longTDateBtn.selected = YES;
//        shortDateBtn.selected = NO;
//        shortTDateBtn.selected = NO;
//        middDateBtn.selected = NO;
//        middTDateBtn.selected = NO;
//        if (self.configureModel.holdDayList.count == 1) {
//
//            self.positionDay = self.configureModel.holdDayList[0];
//        } else if (self.configureModel.holdDayList.count == 2) {
//
//            self.positionDay = self.configureModel.holdDayList[1];
//        } else if (self.configureModel.holdDayList.count == 3) {
//
//            self.positionDay = self.configureModel.holdDayList[2];
//        }
//    }
}

//持仓时间
- (void)clickToShowTimeRecommand{
    
    YZMRecommandView *view = [YZMRecommandView new];
    view.titleLabel.text = @"持仓时间";
    
    NSString *shortDay = [self.configureModel.holdDayList firstObject];
    if (self.configureModel.holdDayList.count > 1) {
        
        NSString *otherDay = [self.configureModel.holdDayList firstObject];
        NSString *longDay = [self.configureModel.holdDayList lastObject];
        view.contentLabel.text = [NSString stringWithFormat:@"持仓时间为%@～%@个交易日，最短%@天，最长%@天。", otherDay, longDay, otherDay, longDay];
    } else {
        
        view.contentLabel.text = [NSString stringWithFormat:@"持仓时间为%@个交易日", shortDay];
    }
    
    [view showInView:self.navigationController.view];
}

//触发止盈
- (void)clickToShowProfitRecommand{
    
    YZMRecommandView *view = [YZMRecommandView new];
    view.titleLabel.text = @"触发止盈";
    view.contentLabel.text = @"当合作交易品种的浮动盈亏达到特定数值时，由投资人即时卖出交易品种全部持有数量进行止盈。";
    [view showInView:self.navigationController.view];
}

//触发止损
- (void)clickToShowLossRecommand{
    
    YZMRecommandView *view = [YZMRecommandView new];
    view.titleLabel.text = @"触发止损";
    view.contentLabel.text = @"当合作交易品种的浮动盈亏达到特定数值时，由投资人即时卖出交易品种全部持有数量进行止损。";
    [view showInView:self.navigationController.view];
}

//递延条件
- (void)clickToShowManagementFeeRecommand{
    
    YZMRecommandView *view = [YZMRecommandView new];
    view.titleLabel.text = @"递延条件";
    view.contentLabel.text = @"当可操盘资金低于递延条件时，只能点卖不能点买。";
    [view showInView:self.navigationController.view];
}

//盈利分成
- (void)clickToShowBondRecommand{
    
    YZMRecommandView *view = [YZMRecommandView new];
    view.titleLabel.text = @"盈利分成";
    view.contentLabel.text = [NSString stringWithFormat:@"点买人按盈利的%@%%进行分成，投资人按盈利的%@%%进行分成。", @(self.configureModel.profitShare * 100).rfString, @(100 - self.configureModel.profitShare * 100).rfString];
    [view showInView:self.navigationController.view];
}

- (void)clickToBuyStock:(UIButton *)send {
    WS(weakSelf);
    NSString *dayString;
    switch (self.scrollowIndex) {
        case 0:
            dayString = self.positionDay;
            break;
        case 1:
            dayString = self.otherPositionDay;
            break;
        default:
            break;
    }
    [YZMDelayCharge showDelayChargeViewWithConfirmHandle:^BOOL{
         [YZMHCMInstance pushViewControllerWithName:@"confirminfor" andParams:@{@"buyMoney":@(weakSelf.buyMoney), @"positionDay":dayString, @"model":weakSelf.configureModel}];
        return  YES;
    } cancleHandle:nil noWaitPayHandle:^{
         [YZMHCMInstance pushViewControllerWithName:@"confirminfor" andParams:@{@"buyMoney":@(weakSelf.buyMoney), @"positionDay":dayString, @"model":weakSelf.configureModel}];
    }];
   
}

- (void)rightBarItemPressed:(id)sender{
    
    [YZMWebViewService openTradeRule:YZM_TRADE_TYPE_FOR_A_STOCK_D];
}

#pragma mark -- UITextFieldDelegate

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    
    YZMGridMoneyModel *model = [self.configureModel.tradingMoneyList lastObject];
    double maxMoney = floor(model.money / 10000) ;
    double minMoney = 1;
    NSNumber *currentAmount = @([textField.text integerValue]);
    
    if (maxMoney == 0) {
        
        return YES;
    }
    
    if ([currentAmount isGreaterThanDouble: maxMoney]) {
        
        [MBProgressHUD showMessage:[NSString stringWithFormat:@"点买金额不能大于%@万", @(maxMoney).r2fString]];
        textField.text = [NSString stringWithFormat:@"%@", @(maxMoney).p2fString];
        self.buyMoney = maxMoney * 10000;
        return YES;
    } else if (![currentAmount isEqualGreaterThanDouble: minMoney]) {
        
        [MBProgressHUD showMessage:[NSString stringWithFormat:@"点买金额不能小于%@万", @(minMoney).r2fString]];
        textField.text = [NSString stringWithFormat:@"%@", @(minMoney).r2fString];
        self.buyMoney = minMoney * 10000;
        return YES;
    } else {
        
        self.buyMoney = [textField.text doubleValue]  * 10000;
    }
    
    return YES;
}

#pragma mark - YZMSegmentViewDelegate
- (void)segmentView:(YZMSegmentView *)segmentView didScrollToIndexView:(NSInteger)index{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    self.scrollowIndex = index;
}

#pragma mark -- NewWork

- (void)fetchStockBuyConfigure{
    
    MBProgressHUD *hud = [MBProgressHUD showMessageManual:@"更新配置参数中"];
    
    __weak __typeof(&*self) weakSelf = self;
    [[YZMTDConfigureService sharedService] fetchConfigureDataSuccess:^{
        
        [hud hide:NO];
        YZMConfigureModel *defaultModel = [YZMTDConfigureService sharedService].defaultConfigure;
        YZMUserServiceInstance.user.assets.balance = defaultModel.balance;
        
        if (![defaultModel.version isEqualToString:weakSelf.configureModel.version]) {
            
            weakSelf.configureModel = [defaultModel copy];
            [weakSelf refreshView];
            
        }else{
            
            weakSelf.configureModel = [defaultModel copy];
        }
        
    } failure:^(NSError *error) {
        
        [hud hide:NO];
        [error yz_showMessage];
    }];
}

@end
